package scu.maqiang.numeric;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class SolverTest {
    public static void main(String[] args) {
        String matrixFileName = "D:\\JFem\\questions21_03\\solverchallenge21_01_A.mtx";
        String rhsFileName = "D:\\JFem\\questions21_03\\solverchallenge21_01_b.rhs";
        SRMatrix A = readSRMatrix(matrixFileName);
        System.out.println(A.getM() + "\t" + A.getN() + "\t" + A.getNNZ());
        double[] rhs = MVO.readArray(rhsFileName);
        System.out.println(rhs.length);
        double[] x = null;
        if (A.isSymmetric()) {
            NewIterSSolver solver = new NewIterSSolver(A);
            x = new double[rhs.length];
            solver.PCGSSOR(rhs, x, 1.5, 2);
        } else {
            NewIterSSolver solver = new NewIterSSolver(A);
            x = new double[rhs.length];
            solver.CGNR(rhs, x, 2);
        }
//        System.out.println(A.isSymmetric());
//        DirectSSolver solver = new DirectSSolver(A);
//        double[] x = solver.LUSolve(rhs, 1, 1.0e-10);
        //System.out.println(MVO.toString(rhs));
    }

    public static SRMatrix readSRMatrix(String fileName) {
        Scanner sc = null;
        try {
            sc = new Scanner(new File(fileName));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        sc.nextLine();
        sc.nextLine();
        sc.nextLine();

        int m = sc.nextInt();
        int n = sc.nextInt();
        int nnz = sc.nextInt();
        SRMatrix my_mat = new SRMatrix(m, n);
        my_mat.mat = new ArrayList[m];
        for (int i = 0; i < m; i++) {
            my_mat.mat[i] = new ArrayList<SREntry>();
        }
        for (int i = 0; i < nnz; i++) {
            int iIdx = sc.nextInt() - 1;
            int jIdx = sc.nextInt() - 1;
            double value = sc.nextDouble();
            my_mat.setElementFast(iIdx, jIdx, value);
        }
        return my_mat;
    }
}
